<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
        <el-form-item label="组" prop="groupName">
          <el-input v-model="formData.groupName" placeholder="请输入组"/>
        </el-form-item>
        <el-form-item label="key值" prop="keyName">
          <el-input v-model="formData.keyName" placeholder="请输入key值"/>
        </el-form-item>
        <el-form-item label="value值" prop="value">
          <el-input v-model="formData.value" placeholder="请输入value值"/>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="formData.description" placeholder="请输入描述" maxlength="100"/>
<!--          <Editor v-model="formData.description" :min-height="192"/>-->
        </el-form-item>
        <el-form-item label="是否有效" prop="status">
          <el-select v-model="formData.status" placeholder="请选择是否有效">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.TURE_OR_FALSE)"
                       :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"/>
          </el-select>
        </el-form-item>
        <!--                    <el-form-item label="是否删除" prop="enable">
                              <el-input v-model="formData.enable" placeholder="请输入是否删除" />
                            </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as ConfigApi from '@/api/system/config';
import Editor from '@/components/Editor';

export default {
  name: "ConfigForm",
  components: {
    Editor,
  },
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        groupName: undefined,
        keyName: undefined,
        value: undefined,
        description: undefined,
        status: 1,
        enable: undefined,
      },
      // 表单校验
      formRules: {
        groupName: [{required: true, message: '组不能为空', trigger: 'change'}],
        keyName: [{required: true, message: 'key值不能为空', trigger: 'change'}],
        value: [{required: true, message: 'value值不能为空', trigger: 'change'}],
        description: [{required: true, message: '描述不能为空', trigger: 'change'}],
        status: [{required: true, message: '状态不能为空', trigger: 'change'}],
      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await ConfigApi.getConfig(id);
          this.formData = res.data;
          this.title = "修改系统配置";
        } finally {
          this.formLoading = false;
        }
      }
      this.title = "新增系统配置";
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await ConfigApi.updateConfig(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await ConfigApi.createConfig(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        groupName: undefined,
        keyName: undefined,
        value: undefined,
        description: undefined,
        status: undefined,
        enable: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
